package cn.kgc.crowdfundingadmin.controller;


import cn.kgc.crowdfundingadmin.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class AdminController {

    @Resource
    AdminService adminService;

  /*  @RequestMapping("/admin/get_by_id")
    @ResponseBody
    public Admin getByID(Integer id) {
        Admin admin = adminService.selectallbyid(id);
System.out.println(admin);
        return admin;
    }*/

    @RequestMapping("/admin/do/login")
    public  String admin(String loginAcct, String userPswd, Model model, HttpSession session){
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin!=null){
            session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
           /* return "admin-main";*/
            return "redirect:/admin/to/main/page.html";
        }else{
            //登录失败
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
    }


    @RequestMapping("/admin/query/for/search")
    //    @PreAuthorize(value="hasAnyAuthority('user:get')")
//    @PreAuthorize(value = "hasAuthority('user:get')")
//    @PreAuthorize(value ="hasRole('超级管理员')")
    @PreAuthorize(value = "hasAnyRole('超级管理员','董事长')")
    public String queryForSearch(  @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                                   @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
                                   @RequestParam(value="keyword", defaultValue="") String keyword,
                                   Model model){
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";

    }


    //删除
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {

            adminService.batchRemove(adminIdList);

            return ResultEntity.successWithoutData();
        }catch(Exception e) {

            return ResultEntity.failed(null, e.getMessage());
        }
    }

    //添加
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {

        adminService.saveAdmin(admin);
            //重定向
        return "redirect:/admin/query/for/search";
    }

    //回显
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }


    //修改
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }


    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }
}
